package com.sgb.capital.view.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.sgb.capital.R;
import com.sgb.capital.base.BaseActivity;
import com.sgb.capital.callback.Constants;
import com.sgb.capital.callback.ZMessageEvent;
import com.sgb.capital.databinding.ConfirmActivityBinding;
import com.sgb.capital.model.OpenBusinessBean;
import com.sgb.capital.utils.Utils;
import com.sgb.capital.view.pop.SeeBigPicture;
import com.sgb.capital.view.pop.TipPop;
import com.sgb.capital.viewmodel.OpenAccountModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import razerdp.basepopup.BasePopupWindow;

/**
 * 作者:张磊
 * 日期:2021/12/14 0014
 * 说明:二次确认页面
 */
public class ConfirmActivity extends BaseActivity {
    ConfirmActivityBinding mBinding;
    OpenAccountModel mModel;
    private TipPop mTipPop;
    private OpenBusinessBean mData;
    private SeeBigPicture mSeeBigPicture;

    public static void start(Context context, String data) {
        Intent intent = new Intent(context, ConfirmActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("data", data);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        Utils.setStatusBarLightMode(this, true);
        mBinding = DataBindingUtil.setContentView(this, R.layout.confirm_activity);
        EventBus.getDefault().register(this);
        mTipPop = new TipPop(mContext);
        mTipPop.setOutSideDismiss(false);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initData() {
        mSeeBigPicture = new SeeBigPicture(this);
        mSeeBigPicture.setBgView(mBinding.grayLayout);
        mModel = new ViewModelProvider(this).get(OpenAccountModel.class);
        mData = new Gson().fromJson(getIntent().getStringExtra("data"), OpenBusinessBean.class);
        mBinding.nsView.setVisibility(View.VISIBLE);
        mBinding.tvTip.setText(Utils.getKeyWordSpan(Utils.getColor(R.color.color_FF4B10), "请您再次确认商户类型是否准确无误，提交后不可修改!", "商户类型"));
        mBinding.tvType.setText(mData.subMerType.contains("3") ? "企业商户" : "个体工商户");
        mBinding.llShow.setVisibility(mData.subMerType.contains("3") ? View.VISIBLE : View.GONE);
        mBinding.tvNameCompany.setText(mData.subMerName);
        mBinding.tvBusinessAddress.setText(mData.address);
        mBinding.tvCreditCode.setText(mData.certId);
        mBinding.tvLegalName.setText(mData.legalName);
        mBinding.tvIdentityId.setText(mData.identityId);
        mBinding.tvContactsName.setText(mData.contactsName);
        mBinding.tvMobileId.setText(mData.mobileId);
        Glide.with(mContext).load(mData.bussinessLicense).into(mBinding.ivBusinessLicense);
        Glide.with(mContext).load(mData.bankAccountLicence).into(mBinding.ivLicenceOpenAccounts);
        Glide.with(mContext).load(mData.idCardFront).into(mBinding.ivIdCardFront);
        Glide.with(mContext).load(mData.idCardBack).into(mBinding.ivIdCardBack);

    }

    @Override
    public void initListener() {
        // TODO: 2021/12/20 0020
        mBinding.ivBusinessLicense.setOnClickListener(v -> mSeeBigPicture.showBigPic(mData.bussinessLicense, mBinding.grayLayout));
        mBinding.ivLicenceOpenAccounts.setOnClickListener(v -> mSeeBigPicture.showBigPic(mData.bankAccountLicence, mBinding.grayLayout));
        mBinding.ivIdCardBack.setOnClickListener(v -> mSeeBigPicture.showBigPic(mData.idCardBack, mBinding.grayLayout));
        mBinding.ivIdCardFront.setOnClickListener(v -> mSeeBigPicture.showBigPic(mData.idCardFront, mBinding.grayLayout));
        mBinding.tvBtn.setOnClickListener(v -> mModel.preAudit(mData, this));
        mBinding.ivBack.setOnClickListener(v -> finish());
        mTipPop.setOnDismissListener(new BasePopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                CapitalActivity.start(ConfirmActivity.this, false);
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe
    public void getCode(ZMessageEvent event) {
        switch (event.code) {
            case Constants
                    .EVENT_REFRESH:
                Utils.sendMsg(Constants.EVENT_HIDE_MAIN, null);
                mTipPop.initStr("恭喜您，资料提交成功\n请等待审核", null, "知道了");
                mTipPop.show();
                break;
        }
    }
}